<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<!DOCTYPE HTML>
<html>
<head>
    <base href="${ctx }/">
     <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <link rel="stylesheet" href="plugin/bootstrap-3.3.7-dist/css/bootstrap.min.css"/>
    <link rel="stylesheet" href="css/bootstrap-datetimepicker.css"/>
    <link rel="stylesheet" href="plugin/bootstrap-table/dist/bootstrap-table.css">
    <!-- <link rel="stylesheet" href="css/zoom.css" media="all" />  -->
    <link rel="stylesheet" href="alert/alert.css">
    <style type="text/css">
      .pad{
    padding-left:35px;
    padding-top:10px;
  } 
input{
    margin-top: 8px;
}
select{
margin-top: 8px;
}
.edui-container{
    margin-top: 8px;
}
    </style>
</head>
<body>
<div class="container-fluid">
    <div class="container row" id="query">
        <div>
            <ul class="breadcrumb">
                <li>
                    <i class="icon-home home-icon"></i>
                    <a href="javascript:void(0)">Cost Management(营销管理)</a>
                </li>
                <li class="active">
                    <a href="view/marketing/corporate_information.jsp">Corporate Accounts List(企业机构信息列表)</a>
                </li>
            </ul>
        </div>
        <div>
            <button id="queryBtn" onclick="update('')" class="btn btn-primary">Add(添加) </button>
            <button id="queryBtn" onclick="del()" class="btn btn-primary">Delete(删除) </button>
        </div>
        <div>
            <div class="form-inline" role="form" method="post" id="queryForm">
                <div class="form-group">
                    <label>Company Name(公司名称):</label>
                    <input type="text" id="company" placeholder="" class="form-control">
                </div>
                <div class="form-group">
                    <label>Company ID(公司ID):</label>
                    <input id='userAccount'  class="form-control"  />
                </div>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <button id="queryBtn" onclick="doQuery1('demo-table',queryParams1())" class="btn btn-primary">Search
                </button><button id="queryBtn" onclick="expost()" class="btn btn-primary">Export
                </button>
            </div>
        </div>
        <br>
    </div>

    <table id="demo-table"></table>
</div>
</body>

<!-- 加载需要的js -->
<script src="js/jquery-3.2.1.min.js"></script>
<script src="plugin/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<script src="js/jquery-bootstrap-pagination.js"></script>
<script src="plugin/bootstrap-table/dist/bootstrap-table.min.js"></script>
<script src="plugin/bootstrap-table/dist/locale/bootstrap-table-zh-CN.min.js"></script>
<!--时间  -->
<script src="js/common.js"></script>
<script src="plugin/layer/layer.js"></script>
<script type="text/javascript" charset="utf-8">

    $(function () {
        initPath("${ctx }");
        initTable('demo-table', queryParams, columns, "user/getEnterpriseList");
    });
    var columns = [{field: '', title: 'Choose', checkbox: true},
        {
            field: 'company',
            title: 'Company Name(公司名称)',
            align: 'center',
            valign: 'middle'
        }/* ,
        {
            field: 'businessLicense',
            title: '营业执照号码',
            align: 'center',
            valign: 'middle'
        } */,
        {
            field: 'firstName',
            title: 'Contact Person(公司联系人)',
            align: 'center',
            valign: 'middle'
        },
        {
            field: 'telephone',
            title: 'Contact Number(联系人电话)',
            align: 'center',
            valign: 'middle'
        },
        {
            field: 'email',
            title: 'Email(邮箱)',
            align: 'center',
            valign: 'middle'
        },
        {
            field: 'userAccount',
            title: 'Company  Account ID(公司账号id)',
            align: 'center',
            valign: 'middle'
        },
        {
            field: 'password',
            title: 'Password(密码)',
            align: 'center',
            valign: 'middle'
        },
        {
            field: 'availableMoney',
            title: 'Unused Voucher Amount(未使用金额)',
            align: 'center',
            valign: 'middle'
        },
        {
            field: 'ubBrankNo',
            title: 'Bank card number(银行卡号)',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
            	if(row.ub!=null){
            	return row.ub.ubBrankNo==null?"":row.ub.ubBrankNo;
            	}
            	return "";
            }
        },
        {
            field: 'afterThree',
            title: 'CVV(银行卡后三位)',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
            	if(row.ub!=null){
            		return row.ub.afterThree==null?"":row.ub.afterThree;
            	}
            	return "";
            }
        },
        {
            field: 'expirationTime',
            title: 'Expiration time(过期时间)',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
            	if(row.ub!=null){
            	return row.ub.expirationTime==null?"":row.ub.expirationTime;
            	}
            	return "";
            }
        },
        {
            field: 'addressLine1',
            title: 'Billing address(账单地址)',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
            	if(row.ub!=null){
            	return row.ub.addressLine1==null?"":row.ub.addressLine1;
		            }
		    	return "";
            }
        },
        {
            field: 'addressCity',
            title: 'Billing address city(账单地址城市)',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
            	if(row.ub!=null){
            	return row.ub.addressCity==null?"":row.ub.addressCity;
		            }
		    	return "";
            }
        },
        {
            field: 'id',
            title: 'Operation(操作)',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
            	sessionStorage.setItem("compor_info"+row.id, JSON.stringify(row));
            	return "<a href='javascript:void(0)' onclick=\"update('"+row.id+"')\">Edit</a><span> &nbsp;&nbsp; </span><a href='javascript:void(0)' onclick=\"senCoupon('"+row.id+"')\">Corporate Voucher Records</a>";
            }
        }
    ];
    //导出
    function expost(){
    	var userAccount = $("#userAccount").val();var company = $("#company").val();
    	window.location.href="user/exportEnterprise?company="+company+"&userAccount="+userAccount
    }
    
    //编辑新增
    function update(id){
    	var title="Add New Corporate Account";var company="";var businessLicense="";
    	var firstName="";var telephone="";var email="";var userAccount="";	var password="";var availableMoney="";var businessesPayCard="";
    	var afterThree="";var expirationTime="";var addressLine1="";var addressCity="";var ubId="";
    	if(id!=""){//新增
    		title="Edit Corporate Account Details";
    		var row=JSON.parse(sessionStorage.getItem("compor_info"+id));
    		company=row.company;
    		businessLicense=row.businessLicense;
    		firstName=row.firstName;
    		telephone=row.telephone;
    		email=row.email;
    		userAccount=row.userAccount;
    		password=row.password;
    		availableMoney=row.availableMoney;
    		businessesPayCard=row.businessesPayCard;
    		if(row.ub!=null){
    			afterThree=row.ub.afterThree;
    			expirationTime=row.ub.expirationTime;
    			addressLine1=row.ub.addressLine1;
    			addressCity=row.ub.addressCity;ubId=row.ub.ubId;
    		}
    	}
     			layer.open({
     	 			  type: 1,
     	 			  title: title,
     	 			  skin: 'layui-layer-rim', //加上边框
     	 			  area: ['620px', '760px'], //宽高
     	 			  content: ' <form id="adds" id="adds"><table>'+
     	 			  '  <tr class="padtr"><td class="pad">Company Name(公司名称):</td> '+
     	 		       '<td><input name="id" id="id" type="hidden" value="'+id+'"/><input name="identity" value="4" id="identity" type="hidden"/><input type="text" class="form-control"  name="company" id="company" value="'+company+'"   ></td></tr>'+
     	 			 /*  '  <tr class="padtr"><td class="pad">营业执照号码:</td> '+
     	        '<td><input type="text" class="form-control" name="businessLicense" id="businessLicense" value="'+businessLicense+'"  placeholder="请输入营业执照号码"></td></tr>'+ */
     	        '  <tr class="padtr"><td class="pad">Contact Person(公司联系人):</td> '+
     	        '<td><input type="text" class="form-control" name="firstName" id="firstName" value="'+firstName+'"/></td></tr>'+
     	       '  <tr class="padtr"><td class="pad">Contact Number(联系人电话):</td> '+
    	        '<td><input type="text" class="form-control" name="telephone" id="telephone" value="'+telephone+'"/></td></tr>'+
    	        
    	        '  <tr class="padtr"><td class="pad">Bank Account(银行卡号):</td> '+
     	        '<td><input type="text" class="form-control"  name="businessesPayCard" id="businessesPayCard" value="'+businessesPayCard+'"/></td></tr>'+
     	        
     	       '  <tr class="padtr"><td class="pad">CVV(卡背后三位):</td> '+
    	        '<td><input type="number" class="form-control" oninput="regCvv(\'afterThree\')"  id="afterThree" value="'+afterThree+'"/></td></tr>'+
    	        '  <tr class="padtr"><td class="pad">Expiration time(卡过期时间):</td> '+
     	        '<td><input type="number"  style="width:60px" oninput="regmonth(\'expirationTime1\')" id="expirationTime1" placeholder="month" value="'+expirationTime+'"/>/<input type="number" placeholder="year" oninput="reg(\'expirationTime2\')"  style="width:60px"  id="expirationTime2" value="'+expirationTime+'"/></td></tr>'+
     	       '  <tr class="padtr"><td class="pad"> Card’s billing address(卡账单地址):</td> '+
    	        '<td><input type="text" class="form-control"   id="addressLine1" value="'+addressLine1+'"/></td></tr>'+
    	        '  <tr class="padtr"><td class="pad">Card’s billing city(卡账单城市):</td> '+
     	        '<td><input type="text" class="form-control" id="addressCity" value="'+addressCity+'"/><input type="hidden" id="ubId"/></td></tr>'+
     	        
     	        
     	       '  <tr class="padtr"><td class="pad">Email(邮箱):</td> '+
    	        '<td><input type="text" class="form-control" name="email" id="email" value="'+email+'"/></td></tr>'+
    	        '  <tr class="padtr"><td class="pad">Login ID:</td> '+
     	        '<td><span>'+userAccount+'</span></td></tr>'+
     	       '  <tr class="padtr"><td class="pad">Password(密码):</td> '+
    	        '<td><input type="text" class="form-control" name="password" id="password" value="'+password+'"/></td></tr>'+
    	        '  <tr class="padtr"><td class="pad">Unused Voucher Amount(可使用金额):</td> '+
     	        '<td><input type="number" class="form-control" name="availableMoney" id="availableMoney"  value="'+availableMoney+'" oninput="reg(\'availableMoney\')"/><span style="margin-left: 194px;  position: fixed; margin-top: -26px;">AUD</span></td></tr>'+
     	        '<tr align="center" class="padtr" ><td colspan="5" style="padding-top:10px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+
     	        ' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+
     	        ' <button type="button" onclick="save('+id+')" class="btn btn-info btn1">&nbsp;Save&nbsp;&nbsp;</button>'+
     	        '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+
     	        '<button type="button" onclick="cacel()" class="btn btn-info btn1">&nbsp;&nbsp;Cancel&nbsp;&nbsp;</button></td></tr></table></form>'
     	 			});
    }  
    function regCvv(id){
    	var text=$('#'+id+'').val();
    	if(text.length>3){
    		 $('#'+id+'').val(text.substring(0,3));
    		 $('#'+id+'').css("border-color","#FF0000");
    	}else if(text.length<3){
    		 $('#'+id+'').css("border-color","#FF0000");
    	}else{
    		$('#'+id+'').css("border-color","#ccc")
    	}
    }
    function regmonth(id){
    	var text=$('#'+id+'').val();
		 if(parseInt(text)<=0||parseInt(text)>12){
			 $('#'+id+'').css("border-color","#FF0000")
		 }else{
			$('#'+id+'').css("border-color","#ccc")
		 }
    }
    function reg(id){
    	var text=$('#'+id+'').val();
		 if(parseInt(text)<=0){
			 $('#'+id+'').css("border-color","#FF0000")
 		 }else{
 			$('#'+id+'').css("border-color","#ccc")
 		 }
    }
    function del(){
    	var row = $('#demo-table').bootstrapTable('getSelections');
    	if(row.length<1){
			 layer.msg("Please select 1 record for editing"); 
			 return;
		}
    	  var s=new Array();
          for(var i in row){
          	s.push(row[i].id);
          }
          var id=s.join(",");
    	var  flag =  confirm("Are you sure you want to delete?");				
		  if(flag){
				 $.ajax({
			            type: "get",
			            url: "user/deleteEnterprise",
			            dataType:"json",
			            data:{"ids":id},
			            success: function(data){    
			           	  console.log(data)
			          	  if(data.code==200){	 
			          		  layer.closeAll();
			          		$('#demo-table').bootstrapTable('refreshOptions',queryParams());
				        	 }else{
				        		  layer.msg(data.msg); 
				        	 }
			       }
			        }); 
		  }
    }
    
    
    //保存企业信息
    function save(){
    	var formData=serialize('adds');
    	if(formData.company==null||formData.company==""){
    		layer.msg("Corporate Information Cannot be empty");
    		return;
    	}
    /* 	if(formData.businessLicense==null||formData.businessLicense==""){
    		layer.msg("营业执照号码不能为空");
    		return;
    	}   */     
    	if(formData.firstName==null||formData.firstName==""){
    		layer.msg("Contact Information cannot be empty");
    		return;
    	}
    	if(formData.telephone==null||formData.telephone==""){
    		layer.msg("Contract Number cannot be empty");
    		return;
    	}
    	if(formData.businessesPayCard==null||formData.businessesPayCard==""){
    		layer.msg("Bank Account details cannot be empty");
    		return;
    	}
    	 if(formData.email==null||formData.email==""){
    		layer.msg("Email address cannot be empty");
    		return;
    	} if(formData.password==null||formData.password==""){
    		layer.msg("Login Password must be filled");
    		return;
    	}
    	if(formData.availableMoney==null||formData.availableMoney==""){
    		layer.msg("Voucher Amount cannot be empty");
    		return;
    	}
    	if(formData.availableMoney<=0){
    		layer.msg("Voucher number cannot be 0");
    		return;
    	}
    	var afterThree=$('#afterThree').val();
    	var expirationTime1=$('#expirationTime1').val();
    	var expirationTime2=$('#expirationTime2').val();
    	var addressLine1=$('#addressLine1').val();
    	var addressCity=$('#addressCity').val();
    	var ubId=$('#ubId').val();
    	if(afterThree==""){
    		layer.msg("CVV cannot be empty");
    		return;
    	}
    	if(afterThree==""){
    		layer.msg("CVV cannot be empty");
    		return;
    	}
    	
    	if(expirationTime1==""){
    		layer.msg("Card expired month cannot be empty");
    		return;
    	}
    	if(expirationTime2==""){
    		layer.msg("Card expired years cannot be empty");
    		return;
    	}
    	if(addressLine1==""){
    		layer.msg("Billing address cannot be empty");
    		return;
    	}
    	if(addressCity==""){
    		layer.msg("Billing address city cannot be empty");
    		return;
    	}
    	var ub={"ubBrankNo":formData.businessesPayCard,"theCardholder":formData.firstName,"afterThree":afterThree,"expirationTime":(expirationTime1+"/"+expirationTime2),
    			"addressLine1":addressLine1,"addressCity":addressCity,"ubId":ubId};
    	formData.ub=ub;
    	console.log(formData);
    	$.ajax({
            type: "post",
            url: "user/addEnterprise",
            data: JSON.stringify(formData),
            contentType:"application/json;charset=UTF-8", 
            dataType:"json",
            success: function(data){    
           	  console.log(data)
          	  if(data.code==200){	
          		layer.msg("Successful");
          		layer.closeAll();
          		 window.location.reload(); 	  
	        	 }else{
	        		  layer.msg(data.msg); 
	        	 }
       }
        }); 
    }
    //发劵记录
	function senCoupon(id){
		window.location.href="view/marketing/corporate_coupons.jsp?qyId="+id;
	}
    function queryParams(params) {
        var param = {
        	company: $('#company').val(),
        	userAccount: $('#userAccount').val(),
            pageNo: this.pageNumber,
            pageSize: this.pageSize
        }
        return param;
    }

    function queryParams1() {
        var param = {
        	company: $('#company').val(),
            userAccount: $('#userAccount').val(),
            pageNo: 1,
            pageSize: this.pageSize
        }
        return param;
    }
</script>
</html>


